﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tour.BLL;
using Tour.DAL;

public partial class Admin_Modules_Room_RoomAE : VST.Controls.BaseLCGPage
{
    public int? RoomId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        GetPropertyFromQueryString();
        if (!IsPostBack)
        {
            LoadDataParentDDL();
            LoadData();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        TblRoomManager.CreateInstant().InsertUpdate(GetTblRoomEntity());
        TblRoomManager.CreateInstant().SortTblRoom(ddlRoomType.SelectedValue.ToInt());
        Response.Redirect("RoomList.aspx");
    }
    void GetPropertyFromQueryString()
    {
        if (Request["RoomId"] != null)
            RoomId = Request["RoomId"].ToInt();
        else
            RoomId = null;
    }
    void LoadDataParentDDL()
    {
        List<TblRoomTypeEntity> ls = TblRoomTypeManager.CreateInstant().SelectTblRoomTypeBy(null,null, WorkingLanguage,"",null,null,"","");
        ddlRoomType.DataSource = ls;
        ddlRoomType.DataTextField = "Name";
        ddlRoomType.DataValueField = "Id";
        ddlRoomType.DataBind();
        ddlRoomType.Items.Insert(0, new ListItem("------Loại Room----", "-1"));
    }
    void LoadData()
    {
        if (RoomId.HasValue)
        {
            TblRoomEntity item = TblRoomManager.CreateInstant().SelectOne(RoomId.Value);
            txtLocation.Text = item.Location;
            imgThumb.ImageUrl = item.Image;
            hdfimgImageTour.Value = item.Image;
            txtName.Text = item.Name;
            txtFacilities.Text = item.Facilities;
            ddlRoomType.SelectedValue = item.RoomTypeId.ToString();
            LiteralCreateBy.Text = item.CreatedBy;
            txtCreateDate.Text = item.CreatedDate.ToString("dd/MM/yyyy");
            lblCreateTime.Text = item.CreatedDate.ToString("HH:mm:ss");           
        }
        else
        {
            txtCreateDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            lblCreateTime.Text = DateTime.Now.ToString("HH:mm:ss");            
        }
    }
    TblRoomEntity GetTblRoomEntity()
    {
        TblRoomEntity a = new TblRoomEntity();
        if (RoomId.HasValue)
        {
            a = TblRoomManager.CreateInstant().SelectOne(RoomId.Value);
        }
        else
        {
            a.TextId = TblRoomManager.CreateInstant().GetUniqueTextIdFromUnicodeText(txtName.Text);
            a.DisplayOrder = 1;
            a.CreatedBy = HttpContext.Current.User.Identity.Name;

        }
        if (txtCreateDate.Text.ToDateTime("vi-VN").Date == new DateTime(1900, 1, 1))
        {
            a.CreatedDate = DateTime.Now;
        }
        else
        {
            string[] str = lblCreateTime.Text.Split(new string[] { ":" }, StringSplitOptions.None);
            a.CreatedDate = txtCreateDate.Text.ToDateTime("vi-VN").AddHours(int.Parse(str[0])).AddMinutes(int.Parse(str[1])).AddSeconds(int.Parse(str[2]));
        }
        a.Name = txtName.Text;
        a.RoomTypeId = ddlRoomType.SelectedValue.ToInt();
        a.Facilities = txtFacilities.Text;
        a.Location = txtLocation.Text;
        a.DisplayOrder = 1;
        a.Image = hdfimgImageTour.Value;       
        return a;
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, int NewIndex)
    {
        string Whereclause = " And RoomTypeId IS NULL ";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And RoomTypeId='{0}'", strParentId);
        }
        string Whereclause1 = String.Format(" Id='{0}'", strId);
        string Whereclause2 = String.Format(" And Id<>'{0}'", strId);
        Tools.SqlHelper.UpdateOrders("TblRoom", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
}